/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.properties.activity;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow.model.binding.ServiceBinding;
import org.fireflow.model.binding.impl.ServiceBindingImpl;
import org.fireflow.model.servicedef.ServiceDef;
import org.fireflow.pdl.fpdl20.process.Activity;
import org.fireflow.pdl.fpdl20.process.WorkflowProcess;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ActivityWrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class ServiceBindingPropertySection  extends AbstractPropertySection {
	ActivityWrapper activityWrapper = null;
	
	ServiceBindingEditPanel serviceBindingEditPanel = null;
	
	
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		serviceBindingEditPanel = new ServiceBindingEditPanel(true);
		serviceBindingEditPanel.setFormToolkit(this.getWidgetFactory());
		serviceBindingEditPanel.createControl(parent);
	}
	@Override
	public void refresh() {
		super.refresh();

		serviceBindingEditPanel.refresh();
		
		
	}
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart){
			activityWrapper = (ActivityWrapper)((AbstractEditPart)obj).getModel();
			
			Activity activity = (Activity)activityWrapper.getWorkflowModelElement();
			ServiceBinding serviceBinding = activity.getServiceBinding();
			if (serviceBinding==null){
				serviceBinding = new ServiceBindingImpl();
				activity.setServiceBinding(serviceBinding);
			}
			SubProcessWrapper subProcessWrapper = activityWrapper.getSubProcessWrapper();
			
			serviceBindingEditPanel.setSubProcessWrapper(subProcessWrapper);
			serviceBindingEditPanel.setInput(serviceBinding);
			serviceBindingEditPanel.setActivityWrapper(activityWrapper);
			WorkflowProcess process = (WorkflowProcess)subProcessWrapper.getWorkflowModelElement().getParent();
			if (serviceBinding!=null && serviceBinding.getServiceId()!=null && !serviceBinding.getServiceId().trim().equals("")){
				ServiceDef svc = process.getService(serviceBinding.getServiceId());
				serviceBindingEditPanel.setServiceDef(svc);
			}else{
				serviceBindingEditPanel.setServiceDef(null);
			}
		}
	}
}
